import {
	defineStore
} from 'pinia';

export const exam = defineStore('exam', {
	state: () => {
		return {
			wrong: false,
			start_time: '', //开始时间
			end_time: '', // 结束时间
			topic: {
				SingleSelect: [],
				MultiSelect: [],
				FillInBlanks: [],
				ShortAnswer: [],
				TrueOrfalse: [],
			}, // 考题
			answer: {
				SingleSelect: [],
				MultiSelect: [],
				FillInBlanks: [],
				ShortAnswer: [],
				TrueOrfalse: [],
			} // 答案
		};
	},
	unistorage: true,
	actions: {
		// 考试格式化
		setTopic(data, time) {
			this.wrong = false;
			this.topic = {
				SingleSelect: [],
				MultiSelect: [],
				FillInBlanks: [],
				ShortAnswer: [],
				TrueOrfalse: [],
			};
			// 格式化考题
			for (let prop in data) {
				// 单选题
				if (data[prop].mark === 'SingleSelect') {
					this.topic.SingleSelect = data[prop];
					// 加入题型答案
					this.setResult(data[prop], 0, 'SingleSelect')
				}
				// 多选题
				if (data[prop].mark === 'MultiSelect') {
					this.topic.MultiSelect = data[prop];
					this.setResult(data[prop], 1, 'MultiSelect')
				}
				// 填空题
				if (data[prop].mark === 'FillInBlanks') {
					this.topic.FillInBlanks = data[prop];
					this.topic.FillInBlanks.question.forEach((item) => {
						item.num = (item.title.length - item.title.replace(/\_+/g, "").length) / 3;
					})
					this.setResult(data[prop], 2, 'FillInBlanks')
				}
				// 简答题
				if (data[prop].mark === 'ShortAnswer') {
					this.topic.ShortAnswer = data[prop];
					this.setResult(data[prop], 3, 'ShortAnswer')
				}
				// 判断题
				if (data[prop].mark === 'TrueOrfalse') {
					this.setResult(data[prop], 4, 'TrueOrfalse')
					this.topic.TrueOrfalse = data[prop];
				}
			}
			// 获取时间
			this.start_time = this.getTime(0);
			this.end_time = this.getTime(time);
		},
		setResult(data, index, type) {
			let result = [];
			data.question.forEach(item => {
				result.push({
					q_id: item.id,
					answer: '',
				})
			})
			this.answer[`${type}`] = {
				type_id: data.type_id,
				result: result
			}
		},
		getTime(time) {
			let date = new Date();
			let min = date.getMinutes();
			date.setMinutes(min + Number(time));
			let y = date.getFullYear();
			let m = (date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : (date.getMonth() + 1);
			let d = date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate();
			let h = date.getHours() < 10 ? ('0' + date.getHours()) : date.getHours();
			let f = date.getMinutes() < 10 ? ('0' + date.getMinutes()) : date.getMinutes();
			let s = date.getSeconds() < 10 ? ('0' + date.getseconds()) : date.getSeconds();
			return y + '-' + m + '-' + d + " " + h + ":" + f + ":" + s;
		},
		// 错题解析格式化
		setWrong(data) {
			this.wrong = true;
			this.topic = {
				SingleSelect: [],
				MultiSelect: [],
				FillInBlanks: [],
				ShortAnswer: [],
				TrueOrfalse: [],
			};
			for (let prop in data) {
				// 单选题
				if (data[prop].mark === 'SingleSelect') {
					this.topic.SingleSelect = data[prop];
					// 加入题型答案
					this.setWrongResult(data[prop], 0, 'SingleSelect')
				}
				// 多选题
				if (data[prop].mark === 'MultiSelect') {
					this.topic.MultiSelect = data[prop];
					this.setWrongResult(data[prop], 1, 'MultiSelect')
				}
				// 填空题
				if (data[prop].mark === 'FillInBlanks') {
					this.topic.FillInBlanks = data[prop];
					this.topic.FillInBlanks.question.forEach((item) => {
						item.num = (item.title.length - item.title.replace(/\_+/g, "").length) / 3;
					})
					this.setWrongResult(data[prop], 2, 'FillInBlanks')	
				}
				// 简答题
				if (data[prop].mark === 'ShortAnswer') {
					this.topic.ShortAnswer = data[prop];
					this.setWrongResult(data[prop], 3, 'ShortAnswer')
				}
				// 判断题
				if (data[prop].mark === 'TrueOrfalse') {
					this.setWrongResult(data[prop], 4, 'TrueOrfalse')
					this.topic.TrueOrfalse = data[prop];
				}
			}
			console.log(data)
		},
		setWrongResult(data, index, type) {
			let result = [];
			data.question.forEach(item => {
				result.push({
					q_id: item.id,
					answer: item.standard_answer,
				})
			})
			this.answer[`${type}`] = {
				type_id: data.type_id,
				result: result
			}
		},
	},
});
